<template>
  <div class="container">
    <el-dialog
      title="添加退货单"
      :visible.sync="addDialog.centerDialogVisible"
      @open="openData"
      @close="cancel"
      center
      width="99%"
    >
      <el-select
        style="margin-bottom: 5px"
        v-model="organization"
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请选择店铺"
        :remote-method="organizationMethod"
        :loading="insides.loading"
      >
        <el-option
          v-for="item in insides.option"
          :key="item.id"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <div>
        <el-select
          style="margin-bottom: 5px"
          v-model="psanization"
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          placeholder="请选择配送中心"
          :remote-method="organizationMethodps"
          :loading="insideps.loading"
        >
          <el-option
            v-for="item in insideps.option"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>

      <div class="top">
        <el-select
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          v-model="commodity"
          placeholder="请搜索选择商品"
          :remote-method="insideRemoteMethod"
          :loading="inside.loading"
        >
          <el-option
            v-for="item in inside.option"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button
          size="medium"
          type="primary"
          class="handleSearch"
          icon="el-icon-search"
          @click="addGoods()"
          >添加</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        max-height="500"
        style="width: 100%"
        empty-text="暂无数据"
      >
        <el-table-column
          prop="goods_name"
          label="商品名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="erp_goods_code"
          label="商品编码"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="unit"
          label="单位"
          align="center"
        ></el-table-column>
        <el-table-column label="箱规格" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.boxspec"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="箱数" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.boxqty"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.qty"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="成本价" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.cost"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="退货价" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.price"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="入库价" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.inp_price"
            ></el-input-number>
          </template>
        </el-table-column>

        <el-table-column label="供应商" align="center" width="150px">
          <template slot-scope="scope">
            <el-select
              :multiple="false"
              filterable
              remote
              center
              reserve-keyword
              v-model="scope.row.supplierName"
              placeholder="请选择供应商"
              :remote-method="supplierRemoteMethod"
              :loading="supplier.loading"
              @change="getSupplier(scope)"
            >
              <el-option
                v-for="item in supplier.options"
                :key="item.id"
                :label="item.label"
                :value="item.label"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="采购员" align="center" width="150px">
          <template slot-scope="scope">
            <el-select
              :multiple="false"
              filterable
              remote
              center
              reserve-keyword
              v-model="scope.row.purchaseName"
              placeholder="请选择采购员"
              :remote-method="purchaseRemoteMethod"
              :loading="purchase.loading"
              @change="getPurchase(scope)"
            >
              <el-option
                v-for="item in purchase.options"
                :key="item.id"
                :label="item.label"
                :value="item.label"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="溢折" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.spillfoldvalue"
            ></el-input-number>
          </template>
        </el-table-column>

        <el-table-column label="调货提成" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.allocateprice"
            ></el-input-number>
          </template>
        </el-table-column>

        <el-table-column label="预提费" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.profitvalue"
            ></el-input-number>
          </template>
        </el-table-column>

        <el-table-column label="筐提成" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.packagingboxprofitvalue"
            ></el-input-number>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="danger"
              @click="remove(scope)"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getOrgData,
  addSaleOrder,
  selectGoodsBatch,
  add,
} from "@/api/middleorder/middleorderlist";
import { getList } from "@/api/putStorage/putStotageList"; //采购员  key: ""limit: 1000
import { supplierList } from "@/api/supplier/supplierList"; //供应商 code: ""  limit: 1000  name: ""
import { selectRdcData } from "@/api/select/select"; //page: 1,  limit: 500, code_or_name: this.search.selectnamecode,配送中心
import { commodityList } from "@/api/commodity/commodityList"; //商品
import { getToken } from "@/utils/auth";
import { Loading } from "element-ui";
export default {
  props: {
    addDialog: Object,
  },
  data() {
    return {
      tableData: [],
      commodity: "",
      is_assign: false,
      inside: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      },
      supplier: {
        options: [],
        value: "",
        list: [],
        loading: false,
      },
      purchase: {
        options: [],
        value: "",
        list: [],
        loading: false,
      },
      insides: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      },
      insideps: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      },
      orgvalue: {
        org_id: "",
        org_code: "",
        org_name: "",
      },
      dcvalue: {
        dcid: "",
        dccode: "",
        dcname: "",
      },
      ispici: false,
      isnum: false,
      OrgDatalist: [],
      dtDatalist: [],
      search: {
        goods_code: "",
        org_code: "",
        ps_code: "",
      },
      organization: "",
      psanization: "",
      priceNum: 0,
    };
  },
  methods: {
    //取消
    cancel() {
      this.addDialog.centerDialogVisible = false;
      this.is_assign = false;
      this.commodity = "";
      this.organization = "";
      this.psanization = "";
      this.tableData = [];
      this.ispici = false;
      this.isnum = false;
      this.OrgDatalist = [];
      this.dtDatalist = [];
      this.inside = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      };
      this.insideps = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      };
      this.insides = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      };
      this.orgvalue = {
        org_id: "",
        org_code: "",
        org_name: "",
      };
      this.dcvalue = {
        dcid: "",
        dccode: "",
        dcname: "",
      };
      this.search = {
        goods_code: "",
        org_code: "",
        ps_code: "",
      };
      this.supplier = {
        options: [],
        value: "",
        list: [],
        loading: false,
      };
      this.purchase = {
        options: [],
        value: "",
        list: [],
        loading: false,
      };
    },

    getData() {
      let data = {
        page: 1,
        limit: 500,
        search: this.search.goods_code,
      };
      //获取商品
      commodityList(data).then((res) => {
        this.goodsList = res.data.data;
        if (res.data.data) {
          this.inside.option = res.data.data.map((item) => {
            return { value: `${item.code}`, label: `${item.name}` };
          });
        }
      });
    },
    openData() {
      this.getDatas();
      this.getDataps();
      this.getData();
      this.openSupplie();
      this.purchaseList();
    },
    getDatas() {
      let data = {
        page: 1,
        limit: 500,
        key: this.search.org_code,
      };
      //获取机构
      getOrgData(data).then((res) => {
        this.OrgDatalist = res.data;
        if (res.data) {
          this.insides.option = res.data.map((item) => {
            return { value: `${item.erp_org_code}`, label: `${item.name}` };
          });
        }
      });
    },
    getDataps() {
      let data = {
        page: 1,
        limit: 500,
        code_or_name: this.search.ps_code,
      };
      //获取配送中心
      selectRdcData(data).then((res) => {
        this.dtDatalist = res.data.paginate.data;
        if (res.data.paginate.data) {
          this.insideps.option = res.data.paginate.data.map((item) => {
            return { value: `${item.d_code}`, label: `${item.d_name}` };
          });
        }
      });
    },
    //删除
    remove(scope) {
      let id = scope.row.erp_goods_code;
      this.tableData.forEach((res, index) => {
        if (res.erp_goods_code == id) {
          this.tableData.splice(index, 1);
        }
      });
      this.priceNum = 0;
    },
    // 添加商品
    addGoods() {
      console.log(this.commodity, 333444);
      if (this.commodity) {
        let every = this.tableData.filter((res) => {
          return (
            res.Code == this.commodity || res.erp_goods_code == this.commodity
          );
        });
        if (every.length == 0) {
          let arr = this.goodsList.filter((res) => {
            return res.code == this.commodity;
          });
          console.log(arr, 88888);
          this.tableData.push({
            erp_goods_code: arr[0].code,
            goods_name: arr[0].name,
            erp_goods_id: arr[0].erp_id,
            goods_id: 0,
            boxspec: 0,
            boxqty: 0,
            qty: 0,
            unit: arr[0].unit,
            cost: 0,
            price: 0,
            inp_price: 0,
            spillfoldvalue: 0,
            allocateprice: 0,
            profitvalue: "",
            packagingboxprofitvalue: 0,
            supplierId: "",
            supplierName: "",
            supplierCode: "",
            purchaseId: "",
            purchaseName: "",
            purchaseCode: "",
          });
          console.log(this.tableData, 22222);
        } else {
          this.$message({
            type: "info",
            message: "该商品已添加",
          });
        }
      } else {
        this.$message({
          type: "info",
          message: "请选择商品",
        });
      }
    },
    //获取供应商
    openSupplie(name = "") {
      let data = {
        page: 1,
        limit: 500,
        name: name,
      };
      supplierList(data).then((res) => {
        this.supplier.options = res.data.data.map((item) => {
          return {
            value: item.id,
            label: item.name,
            code: item.code,
          };
        });
      });
    },
    //获取采购
    purchaseList(name = "") {
      let data = {
        page: 1,
        limit: 500,
        key: name,
      };
      getList(data).then((res) => {
        this.purchase.options = res.data.map((item) => {
          return {
            value: item.id,
            label: item.name,
            code: item.code,
          };
        });
      });
    },
    // 获取商品方法
    insideRemoteMethod(query) {
      if (query !== "") {
        this.getSupplierList(query);
      } else {
        this.inside.options = [];
      }
    },
    //获取商品数据
    getSupplierList(query) {
      this.search.goods_code = query;
      this.getData();
    },
    // 嵌套搜索客户数据
    organizationMethod(query) {
      if (query !== "") {
        this.organizationList(query);
      } else {
        this.insides.option = [];
      }
    },
    //获取客户数据
    organizationList(query) {
      this.search.org_code = query;
      this.getDatas();
    },

    // 嵌套搜索配送中心
    organizationMethodps(query) {
      if (query !== "") {
        this.organizationpsList(query);
      } else {
        this.insideps.option = [];
      }
    },
    //获取配送中心数据
    organizationpsList(query) {
      this.search.ps_code = query;
      this.getDataps();
    },
    //搜索供应商
    supplierRemoteMethod(query) {
      if (query !== "") {
        this.openSupplie(query);
      } else {
        this.supplier.options = [];
      }
    },
    getSupplier(scope) {
      let supplier = this.supplier.options.filter((res) => {
        return res.label == scope.row.supplierName;
      });
      this.tableData[scope.$index].supplierId = supplier[0].value;
      this.tableData[scope.$index].supplierName = supplier[0].label;
      this.tableData[scope.$index].supplierCode = supplier[0].code;
      console.log(this.tableData);
    },
    purchaseRemoteMethod(query) {
      if (query !== "") {
        this.purchaseList(query);
      } else {
        this.purchase.options = [];
      }
    },
    getPurchase(scope) {
      let purchase = this.purchase.options.filter((res) => {
        return res.label == scope.row.purchaseName;
      });
      this.tableData[scope.$index].purchaseId = purchase[0].value;
      this.tableData[scope.$index].purchaseName = purchase[0].label;
      this.tableData[scope.$index].purchaseCode = purchase[0].code;
      console.log(this.tableData);
    },
    //确定
    confirm() {
      this.OrgDatalist.forEach((item) => {
        if (this.organization == item.erp_org_code) {
          this.orgvalue.org_id = item.id;
          this.orgvalue.org_code = item.erp_org_code;
          this.orgvalue.org_name = item.name;
        }
      });

      this.dtDatalist.forEach((item) => {
        if (this.psanization == item.d_code) {
          this.dcvalue.dcid = item.id;
          this.dcvalue.dccode = item.d_code;
          this.dcvalue.dcname = item.d_name;
        }
      });

      if (!this.orgvalue.org_id) {
        this.$message({
          type: "info",
          message: "请选择店铺",
        });
        return;
      }
      if (!this.dcvalue.dcid) {
        this.$message({
          type: "info",
          message: "请选择配送中心",
        });
        return;
      }
      if (this.tableData.length <= 0) {
        this.$message({
          type: "info",
          message: "请选择商品",
        });
        return;
      }
      let arr = [];
      this.tableData.forEach((res) => {
        arr.push({
          erp_id: res.erp_goods_id,
          boxspec: res.boxspec,
          boxqty: res.boxqty,
          qty: res.qty,
          cost: res.cost,
          price: res.price,
          inp_price: res.inp_price,
          spillfoldvalue: res.spillfoldvalue,
          allocateprice: res.allocateprice,
          profitvalue: res.profitvalue,
          packagingboxprofitvalue: res.packagingboxprofitvalue,
          buyer: res.purchaseName,
          buyer_code: res.purchaseCode,
          unit: res.unit,
          supplier_id: res.supplierId,
          supplier_name: res.supplierName,
          supplier_code: res.supplierCode,
        });
      });

      let data = {
        dcid: this.dcvalue.dcid,
        dccode: this.dcvalue.dccode,
        dcname: this.dcvalue.dcname,
        orgid: this.orgvalue.org_id,
        orgcode: this.orgvalue.org_code,
        orgname: this.orgvalue.org_name,
        goods: JSON.stringify(arr),
      };
      // console.log(data, 999);

      add(data).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg || res.message,
          });
          this.$emit("upData");
          this.cancel();
        }
      });
    },
  },
};
</script>
<style scoped>
.pagination {
  text-align: right;
  padding: 10px;
}
.top {
  margin-bottom: 5px;
}
</style>
